/**
 * @file models/instprofit.js 实例盈亏
 * @author
 */

import { routerRedux } from 'dva/router';
import api from '../api';
import moment from 'moment';

const nowDate = moment(new Date()).format('YYYYMMDD');
export default{
	 namespace:"instprofit",
	 state:{
		 begindate:nowDate,
		 enddate:nowDate,
		 pagesize:15,
		 currentpage:1,
		 table:"ifinstprofit",
	 },
	 subscriptions:{
		 setup({dispatch,history}){
			 history.listen(({pathname,query})=>{
					if( pathname === '/achievement/hedge/instprofit' ){

						if(_.isEmpty(query)){
							dispatch({
								type:'getInstprofitList',
								payload: {
									table:"ifinstprofit",
									pagesize:15,
									currentpage:1,
									begindate:nowDate,
									enddate:nowDate
								}
							})
							dispatch({
								type:"setState",
								payload: {
									table:"ifinstprofit",
									pagesize:15,
									currentpage:1,
									begindate:nowDate,
									enddate:nowDate
								}
							})
						}else{
							dispatch({
								type:"setState",
								payload:query
							})
							dispatch({
								type: 'getInstprofitList',
								payload: query
							})
						}
					}
			 })
		 }
	 },
	 effects:{
		 // * whetherRequest({payload}, {call, put,select}) {
			//  const state=yield select(state => state.holding)
			// 	const newObj={}
			// 	for(var i in payload){
			// 		newObj[i]=state[i]
			// 	}
     //
			// 	if(!_.isEmpty(payload)){
			// 		yield put({
			// 			type:'getInstprofitList',
			// 			payload
			// 		})
			// 	}
			// },

			* getInstprofitList({ payload: query }, { call, put }) {
				const response = yield call(api.getInstanceList, query);
				yield put({
					type: 'getListSuccess',
					payload:  response ,
				});
			},
			*changeRouter({payload:query},{call,put}){
				yield put(routerRedux.push({
					pathname:`/achievement/hedge/instprofit`,
					query:{
						...query,
					}
				}))
			},

	},
	 reducers:{
		 setState(state, {payload}) {
				return {
					...state,
					...payload
				}
			},

		 getListSuccess(state, {payload}) {
			 const {data,page} = payload;
			 const status=data ? true:false
				return {
					...state,
					status,
					instprofitlist:data,
					page,
				}
		 },
	 }
 }
